﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Threading;

namespace DataMappers
{
    
    class LoginInfo
    {
        public string username { get; set; }
        public string password { get; set; }
        public string instance { get; set; }
        public string db { get; set; }
    }

    



    public class Connection
    {
        private SqlConnection _conn;
        private LoginInfo _li;
        private string _cnntString;
        private int _connResquests;
        private volatile bool _pulsed;

        public Connection()
        {
            _li = new LoginInfo();
            SetConntStr();
            _connResquests = 0;
            _pulsed = true;
        }
        public void SetUser(string user)
        {
            _li.username = user;
        }
        public void SetPw(string pw) {
            _li.password = pw;
        }
        public void SetInstance(string instance)
        {
            _li.instance = instance;
        }
        public void SetDB(string db)
        {
            _li.db = db;
        }
        private void SetConntStr()
        {
            //if (_li.username == "" || _li.password == "" || _li.db == "") throw new ArgumentException();
            _cnntString = ConfigurationManager.AppSettings["AppConnectionString"];// "Data Source=" + _li.instance + ";Database=" + _li.db + ";User ID=" + _li.username + ";Password=" + _li.password;
            _conn = new SqlConnection(_cnntString);
        }

        public void OpenConnection()
        {
            lock (_cnntString)
            {
                ++_connResquests;
                while (_connResquests > 1)
                {
                    try
                    {
                        Monitor.Wait(_cnntString);
                        if (_pulsed)
                        {
                            _pulsed = false;
                            break; 
                        }
                    }
                    catch (ThreadAbortException ex)
                    {
                        --_connResquests;
                        throw ex;
                    }
                }
                _conn.Open();
            }
            
            
        }
        public void CloseConnection()
        {
            lock (_cnntString)
            {
                --_connResquests;
                if (_connResquests != 0)
                {
                    _pulsed = true;
                    Monitor.Pulse(_cnntString);
                }
                _conn.Close();
            }
        }
        
        public SqlDataReader ExecuteReader(SqlCommand cmd)
        {
            return cmd.ExecuteReader();
        }

        public int ExecuteNonQuery(SqlCommand cmd)
        {
            return cmd.ExecuteNonQuery();
        }

        public object ExecuteScalar(SqlCommand cmd)
        {
            return cmd.ExecuteScalar();
            //int ret = -1;
            //object o = cmd.ExecuteScalar();
            //if (o != null)
            //{
            //    if (int.TryParse(o.ToString(), out ret)) return ret;
            //}
            //return null;
        }

        public SqlConnection getConnection()
        {
            return _conn;
        }
        public SqlTransaction BeginTransaction(SqlCommand cmd)
        {
            return cmd.Transaction = _conn.BeginTransaction();
        }
        //public void CloseTransaction(SqlTransaction tran)
        //{ 
            
        //}

        
    }
}
